<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="formVisible = true">签到管控</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="创建者" value="1"></el-option>
              <el-option label="批次" value="2"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok"/>
      </div>
    </div>
    <el-table stripe style="width: 100%"  border fit highlight-current-row ref="tableDataRef" :data="tableData" :key="key">
      <el-table-column v-if="colData[0].istrue" min-width="50" align="center" prop="id" label="ID"></el-table-column>
      <el-table-column v-if="colData[1].istrue" min-width="50" align="center" prop="admin_name" label="创建者"></el-table-column>
      <el-table-column v-if="colData[2].istrue"  min-width="100" align="center" prop="batch" label="分发批次">
        <template slot-scope="scope">
          <p v-for="(item,index) in scope.row.batch" :key="item.id" style="margin: 0;padding: 0">{{item.batch}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="50" align="center" prop="admin_name" label="设备数"></el-table-column>
      <el-table-column v-if="colData[4].istrue" min-width="50" align="center" prop="admin_name" label="签到管控">
        <template slot-scope="scope">
          <p>{{scope.row.is_sign==1 ? '开启' : '关闭'}}</p>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[5].istrue" min-width="140" align="center" prop="create_time" label="创建日期">
        <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
      </el-table-column>
      <el-table-column label="操作" min-width="200" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="openEdit(scope.row)" v-if="false">编辑</el-button>
            <el-button size="mini" @click="delSignData(scope.row.id)">删除</el-button>
            <el-button size="mini" type="primary" @click="toDetailSign(scope.row.id)">查看</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" background @size-change="handleSizeChange"
                     @current-change="handleCurrentChange" :page-sizes="[10, 50, 100, 200]"
                     layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="listQuery.pageSize">
      </el-pagination>
    </div>

    <el-dialog title="签到详情" :visible.sync="readSnVisible" width="70%">
      <el-table :data="readSnData" border stripe>
        <el-table-column property="id" align="center" label="序号" min-width="50"></el-table-column>
        <el-table-column property="sn" align="center" label="SN" min-width="150"></el-table-column>
        <el-table-column property="batch" align="center" label="批次" ></el-table-column>
        <el-table-column property="classlevel" align="center" label="班级"></el-table-column>
        <el-table-column property="sno" align="center" label="学号" ></el-table-column>
        <el-table-column property="realname" align="center" label="姓名" ></el-table-column>
        <el-table-column property="create_time" align="center" label="签到时间" min-width="150">
          <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
        </el-table-column>
        <el-table-column label="操作" min-width="150" align="center">
          <template slot-scope="scope">
            <el-row>
              <el-button size="mini" type="primary" @click="openDetail(scope.row)">查看</el-button>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <div class="paging">
        <el-pagination :current-page.sync="hasReadQuery.startSize" background @size-change="hasReadSizeChange"
                       @current-change="hasReadCurrentChange" :page-sizes="[10, 50, 100, 200]"
                       layout="total, sizes, prev, pager, next, jumper" :total="hasReadTotal" :page-size="hasReadQuery.pageSize">
        </el-pagination>
      </div>
    </el-dialog>

    <!-- 添加弹窗 -->
    <el-dialog :title="editSubjects== true ? '编辑签到管控': '添加签到管控' " :visible.sync="formVisible" :close-on-click-modal="false" :close-on-press-escape="false" width="40%" @close="addFormclose">
      <el-form :model="addForm"  ref="ruleForm" :rules="rules" class="diolog_adapt" style="width: 600px" >
        <el-form-item label="批次:" label-width="130px" prop="batch">
          <el-select v-model="addForm.batch"  multiple filterable placeholder="请选择批次" clearable>
            <el-option
              v-for="item in batchArr"
              :key="item.id"
              :label="item.batch"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="签到功能:" label-width="130px" prop="is_sign">
          <el-switch
            v-model="addForm.is_sign"
            active-text="开启"
            inactive-text="关闭"
            :active-value="1"
            :inactive-value="-1">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="formVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" size="mini" round @click="editSign('ruleForm')">确 定
        </el-button>
        <el-button v-else type="primary" @click="addSign('ruleForm')" size="mini" round>确定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="签到详情" :visible.sync="detailVisible" width="40%" @close="detailFormClose()">
      <el-form v-model="detailForm">
        <el-form-item label="签到时间:" label-width="120px">
          <span>{{detailForm.signTime}}</span>
        </el-form-item>
        <el-form-item label="签到类型:" label-width="120px">
          <span>{{detailForm.type}}</span>
        </el-form-item>
        <el-form-item label="签到对象:" label-width="120px">
          <span>{{detailForm.target}}</span>
        </el-form-item>
        <el-form-item label="签到描述:" label-width="120px">
          <span>{{detailForm.describe}}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
  import objectable from "@/utils/objectable.js";
  import dynamicTable from "@/components/DynamicTable";
  import deleteDialog from "@/components/deleteDialog";
  import tableMini from '@/utils/tableMini.js'
  import {
    getSubmitSn,
    addSignControl,
    querySignControl,
    editSignControl, delSignControl, getSignSn
  } from "@/api/launcherMade";
  import {getBatchTwo} from "@/api/equipment";

  export default {
    mixins: [tableMini],
    components: {dynamicTable, deleteDialog},
    data() {
      return {
        key: 1,
        colData: [], //动态控制table
        checkboxVal: ["ID", "创建者", "批次", "设备数", "签到管控", "创建日期"],
        searchTable: "",
        select: "", //搜索条件
        tableData: [],
        listQuery: {
          startSize: 1,
          pageSize: 10,
          userId: localStorage.getItem("ID"),
        },
        hasReadQuery: {
          startSize: 1,
          pageSize: 10,
          id: undefined
        },
        addForm: {
          batch: [],  //批次
          is_sign: 1,
        },
        formVisible: false,
        rules: {
          is_sign: [{ required: true, message: '请配置签到管控开关', trigger: 'change' }],
          batch: [{ required: true, message: '请选择批次', trigger: 'change' }],
        },
        editSubjects: false,
        deleteDialogs: { show: false, deleteText: '', deleteId: 0 },
        batchArr: [],
        readSnVisible: false,
        readSnData: [],
        hasReadTotal: 0,
        detailVisible: false,
        detailForm: {
          signTime: '',  //签到时间
          type: '',  //签到类型
          target: '',  //签到对象
          position: '',  //定位
          describe: '',  //签到描述
        },
      }
    },
    created() {
      this.colData = objectable(this.checkboxVal, this.checkboxVal); //转为数组对象，好控制table
      this.getSelfSignTableData()
      this.getBatchTwo()
    },
    methods: {
      Refresh() {
        this.listQuery = { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), name: undefined}
        this.getSelfSignTableData()
      },
      getSnSignData() {
        getSubmitSn(this.hasReadQuery).then(res => {
          this.readSnData = res.data
          this.hasReadTotal = res.total
        }).catch(err => {
          this.readSnData = []
          this.hasReadTotal = 0
        })
      },
      toDetailSign(id) {
        this.readSnVisible=true
        this.hasReadQuery.id=id
        getSignSn(this.hasReadQuery).then(res => {
          this.readSnData = res.data
        }).catch(err => {
        })
      },
      openDetail(row) {
        this.detailVisible=true
        this.detailForm = {
          signTime: row.signTime,  //签到时间
          type: row.sign_type,  //签到类型
          target: row.target,  //签到对象
          position: row.position,  //定位
          describe: row.describe,  //签到描述
        }
      },
      //搜索,1:课程名称
      idnameSearch() {
        if (!this.select) return this.$message.warning("请先选择查询条件");
        this.listQuery.startSize = 1
        this.listQuery.pageSize = 10
        switch (this.select) {
          case '1':
            this.listQuery.admin_name = this.searchTable
            this.getSelfSignTableData();
            break;
          case '2':
            this.listQuery.batch = this.searchTable
            this.getSelfSignTableData();
            break;
        }
      },
      getBatchTwo() {
        getBatchTwo({ userId: localStorage.getItem("ID") }).then(res => {
          this.batchArr = res.data
        }).catch(err => {
          this.batchArr = []
        })
      },
      openEdit(row) {
        this.formVisible = true
        this.editSubjects=true
        this.addForm = {
          id: row.id,
          batch: row.batchArr,  //批次
          is_sign: row.is_sign
        }
      },
      addSign(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let addForm = JSON.parse(JSON.stringify(this.addForm))
            addForm.last_ip = sessionStorage.getItem("Ip")
            addForm.userId = localStorage.getItem("ID")
            addSignControl(addForm).then(res => {
              this.$message.success(res.msg)
              this.getSelfSignTableData()
              this.formVisible = false
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      getSelfSignTableData() {
        querySignControl(this.listQuery).then(res => {
          this.tableData = res.data
          this.total = res.total
        }).catch(err => {
          this.tableData = []
          this.total = 0
          this.$message.error(res.msg)
        })
      },
      editSign(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            let editForm = JSON.parse(JSON.stringify(this.addForm))
            editForm.last_ip = sessionStorage.getItem("Ip")
            editForm.userId = localStorage.getItem("ID")
            editSignControl(editForm).then(res => {
              this.$message.success(res.msg)
              this.formVisible = false
              this.getSelfSignTableData()
            }).catch(err => {
            })
          } else {
            return false;
          }
        });
      },
      delSignData(id) {
        this.deleteDialogs.show = true
        this.deleteDialogs.deleteId = id
      },
      //删除按钮事件
      deleteEvents() {
        let params = {
          userId: localStorage.getItem("ID"),
          last_ip: sessionStorage.getItem("Ip"),
          id: this.deleteDialogs.deleteId,
        }
        delSignControl(params).then(res => {
          this.$message.success(res.msg)
          this.getSelfSignTableData()
        }).catch(err => {
          console.log("请求失败", err)
        })
      },
      //分页
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.getSelfSignTableData()
      },
      handleCurrentChange(val) {
        this.listQuery.startSize = val
        this.getSelfSignTableData()
      },
      hasReadSizeChange(val) {
        this.hasReadQuery.pageSize = val
        this.getSnSignData()
      },
      hasReadCurrentChange(val) {
        this.hasReadQuery.startSize = val
        this.getSnSignData()
      },
      //下拉动态table子组件
      alterCheckbok(key, conte) {
        this.key = key;
        this.colData.filter(i => {
          if (conte.indexOf(i.title) != -1) {
            i.istrue = false;
            this.$nextTick(() => {
              this.$refs.tableDataRef.doLayout();
            });
          } else {
            i.istrue = true;
            this.$nextTick(() => {
              this.$refs.tableDataRef.doLayout();
            });
          }
        });
      },
      //编辑添加表单关闭事件
      addFormclose() {
        this.editSubjects=false
        this.$nextTick(() => {
          this.addForm = this.$options.data().addForm
        })
      },
      detailFormClose() {
        this.detailVisible=false
        this.$nextTick(() => {
          this.detailForm = this.$options.data().detailForm
        })
      }
    }
  }
</script>



